package com.eight.cloud.mybatisplus.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.cloud.mybatisplus.entity.BaseEntity;

import java.io.Serializable;

/**
 * @ClassName: BaseService
 * @Author: TXC
 * @Date: 2024-10-30 17:01
 **/
public class BaseService<M extends BaseMapper<T>, T extends BaseEntity<T>> extends ServiceImpl<M, T>  implements IBaseService<T>{
    @Override
    public T getByUuid(Serializable uuid) {
        return lambdaQuery().eq(T::getUuid, uuid).one();
    }
    @Override
    public boolean updateByUuid(T entity) {
        return update(entity, lambdaUpdateWrapper().eq(T::getUuid, entity.getUuid()));
    }
    @Override
    public boolean removeByUuid(Serializable uuid) {
        return remove(lambdaQueryWrapper().eq(T::getUuid, uuid));
    }
    @Override
    public LambdaQueryWrapper<T> lambdaQueryWrapper() {
        return Wrappers.lambdaQuery(getEntityClass());
    }
    @Override
    public LambdaUpdateWrapper<T> lambdaUpdateWrapper() {
        return Wrappers.lambdaUpdate(getEntityClass());
    }
    @Override
    public T convertToEntity(Object source, String... ignoreProperties) {
        return BeanUtil.copyProperties(source, getEntityClass(), ignoreProperties);
    }
    @Override
    public <E> E convertToObject(T source, Class<E> target, String... ignoreProperties) {
        return BeanUtil.copyProperties(source, target, ignoreProperties);
    }

}
